﻿using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace ParkService.Host.EntityFrameworkCore
{
    /// <summary>
    /// 停哪儿服务迁移数据上下文工厂
    /// </summary>
    public class ParkServiceMigrationDbContextFactory : IDesignTimeDbContextFactory<ParkServiceMigrationDbContext>
    {
        public ParkServiceMigrationDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();
            var builder = new DbContextOptionsBuilder<ParkServiceMigrationDbContext>()
                .UseSqlServer(configuration.GetConnectionString("ParkManagement"));
            return new ParkServiceMigrationDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);
            return builder.Build();
        }
    }
}